(function (window) {
  class CusEvent {
    constructor(props) {
      this.eventList = {
        // eventname: {
        //   params: [], //事件参数
        //   handler: () => {}, //事件句柄,
        //   customEV: null // 自定义事件对象
        // }
      };
      this.init();
    }

    init() {
      if (!window.CustomEvent) {
        window.CustomEvent = function (type, config) {
          config = config || {
            bubbles: false,
            cancelable: false,
            detail: undefined
          };
          var e = document.createEvent("CustomEvent");
          e.initCustomEvent(
            type,
            config.bubbles,
            config.cancelable,
            config.detail
          );
          return e;
        };
        window.CustomEvent.prototype = window.Event.prototype;
      }
    }

    //判断事件名是否存在
    _hasReigister(eventname) {
      let hasReigister = false;
      Object.keys(this.eventList).forEach(v => {
        if (v === eventname) {
          hasReigister = true;
        }
      });

      if (hasReigister) {
        return true;
      } else {
        return false;
      }
    }

    _registerEvent(eventname, fn) {
      window.addEventListener(eventname, fn, false);
    }

    //绑定事件
    winOn(eventname, fn) {
      let _this = this;

      //绑定事件句柄
      let f = () => {
        fn.apply(null, this.eventList[eventname].params);
      };

      if (this._hasReigister(eventname)) {
        this.eventList[eventname].eventArray.push(f); //如果已有事件，将当前注册事件句柄推进数组
      } else {
        this.eventList[eventname] = {
          // handler: () => {},
          eventArray: [f], // 如果不存在则初始化一个数组，将事件列表存入
          params: [], //事件参数
          customEV: new CustomEvent(eventname)
        };
      }

      this._registerEvent(eventname, f); //注册事件

      //返回解除事件函数
      return () => {
        let index = this.eventList[eventname].eventArray.indexOf(f);
        window.removeEventListener(eventname, f, false);
        this.eventList[eventname].eventArray.splice(index, 1)
      };
    }

    //发送事件
    winEmit(eventname) {
      if (!this._hasReigister(eventname)) {
        console.warn(`${eventname} 事件名未注册`);
        return false;
      }

      this.eventList[eventname].params = [
        ...Array.prototype.slice.call(arguments, 1)
      ];
      window.dispatchEvent(this.eventList[eventname].customEV);
    }

    //解除自定义事件
    winOff(eventname) {
      if (!this._hasReigister(eventname)) {
        console.warn(`${eventname} 事件名不存在`);
        return false;
      }
      let deleteName = "";
      Object.keys(this.eventList).forEach(v => {
        if (v === eventname) {
          deleteName = v;
        }
      });

      if (
        this.eventList[deleteName].eventArray instanceof Array &&
        this.eventList[deleteName].eventArray.length > 0
      ) {
        this.eventList[deleteName].eventArray.forEach(v => {
          window.removeEventListener(eventname, v, false);
        });
      }

      delete this.eventList[deleteName]
    }
  }

  const WE = new CusEvent();
  if (!window.WE) {
    window.WE = WE;
  }
})(window);
